﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxTypeOrg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListtbtOrgType(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementTypeOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách loại tổ chức" });
            try
            {
                tbOrgType_Proxy pr = new tbOrgType_Proxy();
                List<DataAccess.tbOrgType> list = new List<DataAccess.tbOrgType>();
                
                int totalitem = 0;
                if (keyword=="null"||keyword=="")
                {
                    list = pr.SelectAlltbOrgType_Proxy();
                    totalitem = list.Count;
                }
                else
                {
                    list = (pr.SelectNo_tbOrgType_Proxy(keyword)).ToList<DataAccess.tbOrgType>();
                }

                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
               
                return new JavaScriptSerializer().Serialize(new { Status = "Success", Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error", Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteOrgType(int Id)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementTypeOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa loại tổ chức" });
            try
            {
                tbOrg_Proxy pr = new tbOrg_Proxy();
                if (pr.Delete_tbOrg_Proxy(Id))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string InsertOrgType(string NameTypeOrg)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementTypeOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm loại tổ chức" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbOrgType).Name);
                tbOrgType_Proxy pr = new tbOrgType_Proxy();
                DataAccess.tbOrgType a = new DataAccess.tbOrgType();
                a.TypeOrgID = 0;
                a.NameTypeOrg = NameTypeOrg;
                if (pr.Insert_tbOrgType_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Type Org thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string UpdateOrgType(int TypeOrgID, string NameTypeOrg)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementTypeOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa loại tổ chức" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbOrgType).Name);
                tbOrgType_Proxy pr = new tbOrgType_Proxy();
                DataAccess.tbOrgType a = new DataAccess.tbOrgType();
                a.TypeOrgID = TypeOrgID;
                a.NameTypeOrg = NameTypeOrg;
                if (pr.Update_tbOrgType_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật OrgType thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
    }
}